<?php
/*
+ ---------------------------------------------------------------------- +
| e107Shop - An e-commerce plugin for the e107 content management system |
|                                                                        |
| ByOrder Europe 2007-2009                                               |
| http://e107shop.com                                                    |
| pbielen@gmail.com                                                      |
|                                                                        |
| Released under the terms and conditions of the LGPL                    |
+ ---------------------------------------------------------------------- +
*/

// Include e107 class, Header and the e107Shop classes
require_once("../../class2.php");
require_once("library/api.php");

require_once("shortcodes/navigator_shortcodes.php");
require_once("shortcodes/category_shortcodes.php");
if (file_exists(THEME."navigator.php")) require_once(THEME."navigator.php");
else require_once("templates/navigator.php");
if (file_exists(THEME."category_list.php")) require_once(THEME."category_list.php");
else require_once("templates/category_list.php");

require_once(HEADERF);

// check if there is a category provided
if (!empty($_GET['cat'])) {
    $catid = $tp->toDB($_GET["cat"]);
    $whereclause = "parid = '{$catid}' and active=1";
}
else {
    $whereclause = "active=1";
    $whereMainCats = "parid='' and active=1";
}

// Count the categories, if there is only one category, display the products, else display categories
// we use select instead of count here, cause we want to fetch if more than one cat avoiding an extra db call
$countcats = $sql -> db_Select($e107shop_db_name['categories'], "*", $whereclause." AND (category_class IN (".USERCLASS_LIST.")) order by catord");

// if there is only one cat, display the products right away, avoiding an extra click
if ($countcats == 1) {
    $row = $sql -> db_Fetch(MYSQLI_ASSOC);
    $catid = $row['catId'];
    header("Location:products.php?cat=".$catid);
} else {
    // if there is a catid provided just build the category list else do the other checks
    if ($catid) {
        while ($row = $sql -> db_Fetch(MYSQLI_ASSOC)) $catlist[] = $row;
    } else {
        // first check if there are more maincats, if so, then display them, otherwise set catid to the maincat to display the subcats
        $countMainCats = $sql2 -> db_Select($e107shop_db_name['categories'], "*", $whereMainCats." AND (category_class IN (".USERCLASS_LIST.")) order by catord");
        if ($countMainCats > 1) {
            while ($row = $sql2 -> db_Fetch(MYSQLI_ASSOC)) $catlist[] = $row;
        } else {
            $row = $sql2 -> db_Fetch(MYSQLI_ASSOC);
            $mainCatid = $row['catId'];
            $sql->db_Select($e107shop_db_name['categories'], "*", "parid='".$mainCatid."' AND (category_class IN (".USERCLASS_LIST.")) order by catord");
            while ($row = $sql -> db_Fetch(MYSQLI_ASSOC)) $catlist[] = $row;
        }
    }
    cachevars('categorylist', $catlist);
    if ($catid) {
        // Start the row for the navigation
        $sql->db_Select($e107shop_db_name['categories'], "*", "catId='".$catid."' AND (category_class IN (".USERCLASS_LIST."))");
        $row = $sql->db_Fetch();
        $parentid = $row['parid'];
        $navigation[0] = $row['catName'];
        $counter = 1;
        while (!$parentid == '') {
            $sql->db_select($e107shop_db_name['categories'], "*", "catId='".$parentid."' AND (category_class IN (".USERCLASS_LIST.")) order by catord");
            while ($row = $sql->db_Fetch(MYSQLI_ASSOC)) {
                $parentid = $row['parid'];
                $navigation[$counter] = $row['catId'];
                $counter++;
                $navigation[$counter] = $row['catName'];
                $counter++;
            }
        }
        cachevars('e107shopnavigationlist', $navigation);
        $text .= $tp->parseTemplate($NAVIGATOR_TEMPLATE, FALSE, $navigator_shortcodes);
    }
}

// here we display the store info, but only when entering the store.
// So if you want to provide more store information, like easyshop has create a custom top page on index for it
if (!strchr(e_QUERY, "cat=")) e107shop_display_custom_page_info(e_PAGE, "top");

// Render a table with the categories for the customer, display message when there are none
if (!$countcats) {
    $ns->tablerender($shop_settings["store_name"], lan_global_no_goods);
} else {
    // build the category list and display it
    $text .= $tp->parseTemplate($CATEGORIES_TEMPLATE, FALSE, $category_shortcodes);
    // Display navbar only when there are more than 1 active category and at least the catid is present
    if ($countcats > 1 && $catid) $text .= $tp->parseTemplate($NAVIGATOR_TEMPLATE, FALSE, $navigator_shortcodes);
    $ns->tablerender($shop_settings["store_name"], $text);
}

e107shop_display_custom_page_info(e_PAGE, "bottom");

require_once(FOOTERF);
